package com.netease.nie.facade;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.netease.nie.base.cache.redis.Redis;
import com.netease.nie.base.utils.CopyUtils;
import com.netease.nie.base.utils.JsonUtils;
import com.netease.nie.base.utils.PageUtils;
import com.netease.nie.common.CommonBusinessTools;
import com.netease.nie.constants.CachePrefixConstants;
import com.netease.nie.constants.CommonConstants;
import com.netease.nie.constants.DisplayTypeConstants;
import com.netease.nie.dto.ExchangeGoodsInfoListDto;
import com.netease.nie.dto.LimitedTimeExchangeCacheDto;
import com.netease.nie.dto.LimitedTimeExchangeDto;
import com.netease.nie.dto.LimitedTimeExchangeGoodsInfoDto;
import com.netease.nie.model.ColumnInfo;
import com.netease.nie.service.ColumnInfoGoodsService;
import com.netease.nie.service.ColumnInfoService;
import com.netease.nie.service.LimitedTimeExchangeService;

/**
 * 
 * @author linzijie
 * @createDate Nov 25th,2016
 *
 */
@Service
public class LimitedTimeExchangeGoodsFacadeImpl extends CommonBusinessTools implements LimitedTimeExchangeGoodsFacade {
	
	
	private static final String LIMITED_TIME_EXCHANGE_GOODS_PREFIX = CachePrefixConstants.LimitedTimeExchangeGoodsPrefix.LIMITED_TIME_EXCHANGE_GOODS_PREFIX;
	
	@Autowired
	private LimitedTimeExchangeService limitedTimeExchangeService;
	
	@Autowired
	private Redis redis;
	
	public LimitedTimeExchangeDto listLimitTimeExchangeDto(){		
		LimitedTimeExchangeCacheDto limitedTimeExchangeCacheDto;
		LimitedTimeExchangeDto LimitedTimeExchangeDto = new LimitedTimeExchangeDto();
		String limitedTimeExchangeKey = LIMITED_TIME_EXCHANGE_GOODS_PREFIX;
		Long currentTime = System.currentTimeMillis();
		boolean exists = redis.exists(limitedTimeExchangeKey);
		if (!exists) {
			limitedTimeExchangeCacheDto = limitedTimeExchangeService.queryLimitedTimeExchange();
				
			if(limitedTimeExchangeCacheDto != null){
				String text = JsonUtils.obj2String(limitedTimeExchangeCacheDto);	
				redis.set(limitedTimeExchangeKey, text);					
			}else return null;
		} else {
			String text = redis.get(limitedTimeExchangeKey);
			if (text == null || text.isEmpty())
					return null;
			else {
				limitedTimeExchangeCacheDto = JsonUtils.getClazz(text, LimitedTimeExchangeCacheDto.class);
			}
		}
		CopyUtils.copy(LimitedTimeExchangeDto, limitedTimeExchangeCacheDto);
		
		if(limitedTimeExchangeCacheDto.getThisPutOffTime() != null){       //设置倒数时间
			LimitedTimeExchangeDto.setPutOffCountDown(limitedTimeExchangeCacheDto.getThisPutOffTime() - currentTime);						
		}else if(limitedTimeExchangeCacheDto.getNextPutOnTime() != null){
			LimitedTimeExchangeDto.setPutOnCountDown(limitedTimeExchangeCacheDto.getNextPutOnTime() - currentTime);		
		}
			
		replaceLimitedTimeExchangeDtoThumbnailUrl(LimitedTimeExchangeDto, DisplayTypeConstants.WAP.getCode());	
		return LimitedTimeExchangeDto;
	}

}
